package pl.puntin.punclient.view;

import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import pl.puntin.punclient.controller.Controller;
import pl.puntin.punclient.messages.ImageMessage;
import pl.puntin.punclient.messages.RoomListMessage;
import pl.puntin.punclient.view.game.GameView;


/*
 * Klasa ktora bedzie zarzadzala widokami
 * 
 * LUKASZ CZYTAJ TO !!!!!!!!!!!!!!!!!!!!!!!!!
 * ta klasa komunikuje sie WYLACZNIE  z controlerem
 * pamietaj aby Controller wolal metody tej klasy a nie np loginView
 * 
 * w aplikacji jest tylko jeden JFrame !
 * reszta widokow to JPanel
 */
public final class View
{
	private final JFrame applicationWindow;
	private final Controller controller;
	private final GameView gameView;
	private final LoginView2 loginView;
	private final RoomView roomView;
	private JPanel currentView;
	
	public View(final Controller controller)
	{
		this.controller = controller;
		
		applicationWindow = new JFrame("Gra");
		
		loginView = new LoginView2(controller);
		roomView = new RoomView(controller);
		gameView = new GameView(controller);
		setLoginView();
		applicationWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		applicationWindow.pack();
		applicationWindow.setVisible(true);
	}
	
	public void setLoginView()
	{
		currentView = loginView;
		applicationWindow.setContentPane(loginView);
		applicationWindow.pack();
	}
	
	public void changeToRoomView()
	{
		currentView= roomView;
		applicationWindow.setContentPane(roomView);
		applicationWindow.pack();
	}
	
	public void changeToGameView()
	{
		currentView = gameView;
		applicationWindow.setContentPane(gameView);
		applicationWindow.pack();
	}
	
	public void displayErrorMessage(final String message)
	{
		System.err.println(message+" "+new Date());
	}
	
	public void displayLogMessage(final String message)
	{
		System.out.println(message+" "+new Date());
	}
	
	public void displayChatMessage(final String message)
	{
		/*
		 * Tutaj np powino byc
		 * if(currentView != gameView)
		 * 		throw blad lo jezu co sie dzieje
		 * else
		 * 		gameView.showChatMessasge :P
		 */
		System.out.println(message);
	}
	
	/**
	 * Ustawia gameView w tryb rysujacego
	 */
	public void youAreDrawer()
	{
		gameView.startSendingImage();
	}
	
	private void displayImage()
	{
		
	}

	public void displayImageMessage(final ImageMessage message)
	{
		gameView.displayImage(message.getBufferedImage());
		
	}
	
	public void roomListDisplay(final RoomListMessage message)
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				roomView.setList(message.roomList , message.roomPlayers);
							
			}
		});
	}
}
